import {defineStore} from 'pinia'
import type {AttributeAndValuesDTO, Sku,SkuAndSaleAttributeDTO} from "@/api/serve/re-market/type";
import {reqCreateGoods} from '@/api/serve/re-market'

export const useGoodsFormStore = defineStore('goods-form', {
    state: () => {
        return {
            addGoodsForm: {
                spuInfo: {
                    spuName: '',
                    spuDescription: '',
                    categoryId: '',
                    brandName: '',
                },
                groupAttributeList: [] as AttributeAndValuesDTO[],
                saleAttributeList: [] as AttributeAndValuesDTO[],
                skuList: [] as SkuAndSaleAttributeDTO[],
            },
            step: 0,
        }
    },
    getters: {},
    actions: {
        setSpuInfo(spuInfo: any) {
            Object.assign(this.addGoodsForm.spuInfo, spuInfo)
        },
        setGroupAttributeList(groupAttributeList: any) {
            this.addGoodsForm.groupAttributeList =[... groupAttributeList]
        },
        setSaleAttributeList(saleAttributeList: any) {
            this.addGoodsForm.saleAttributeList =[... saleAttributeList]
        },
        setSkuList(skuList: any) {
            this.addGoodsForm.skuList =[... skuList]
        },
        next() {
            if (this.step<5){
                this.step++
            }
        },
        back() {
            if (this.step>0 && this.step<5){
                this.step--
            }
        },
        async submitForm  () {
           let res=await reqCreateGoods(this.addGoodsForm)
            if (res.code === 200) {
                this.step++
            }
        },
    }
    ,
    persist: {
        storage: sessionStorage,    //修改存储位置
        key: 'goods-form',   //设置存储的key,在这里是存在sessionStorage时的键
        pick: ['addGoodsForm','step'],//指定要长久化的字段
    },
})

